/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-12-27 19:44:20
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../extends/timeFormart");
class BrokerService extends Service {
    //获取列表
    async getGroupList() {
            const { ctx, app } = this;
            let res = await app.mysql.select("grouplist");
            let total = res.length;
            res = res.map((item, index) => {
                return {
                    ...item,
                    key: index + 1,
                };
            });
            return {
                total,
                data: res,
            };
        }
        //搜索
    async searchGroupList() {
            const { ctx, app } = this;
            const { name, state, address, keyword, status } = ctx.query;
            let res = await app.mysql.select("grouplist"),
                total = res.length;
            if (name) {
                res = res.filter((item) => item.name.includes(name));
                total = res.length;
            }
            if (address) {
                res = res.filter((item) => item.address.includes(address));
                total = res.length;
            }
            if (status !== undefined) {
                res = res.filter((item) => item.status === status);
                total = res.length;
            }
            if (state !== undefined) {
                res = res.filter((item) => item.state - 0 === state - 0);
                total = res.length;
            }
            if (keyword) {
                res = res.filter((item) => item.name.includes(keyword));
                total = res.length;
            }
            return {
                total,
                data: res,
            };
        }
        //删除
    async delGroupList() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.delete("grouplist", { id });
            return res;
        }
        //改变状态
    async changeGroupState() {
            const { ctx, app } = this;
            const { id } = ctx.request.body;
            const res = await app.mysql.get("grouplist", { id });
            const state = res.state * 1;
            const result = await app.mysql.update("grouplist", { id, state: !state });
            return result;
        }
        //改变开团状态
    async changeOpenState() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        const res = await app.mysql.get("grouplist", { id });
        if (res.status === "已开团") {
            await app.mysql.update("grouplist", { id, status: "未开团" });
        } else {
            await app.mysql.update("grouplist", { id, status: "已开团" });
        }
        return "操作成功";
    }
}
module.exports = BrokerService;